package com.jike.mahjong.service.Imp;

import com.jike.mahjong.common.Page;
import com.jike.mahjong.common.util.PropertyUtil;
import com.jike.mahjong.task.MsgTask;
import com.jike.mahjong.task.TaskUtil;
import com.jike.mahjong.dao.MessageDao;
import com.jike.mahjong.model.MessageModel;
import com.jike.mahjong.model.Version;
import com.jike.mahjong.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by star on 2016/10/14.
 */
@Service("messageService")
public class MessageServiceImp implements MessageService {

    @Autowired
    private MessageDao messageDao;

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    public int NEW_CHECK_INTERVAL = 2 * 1000;

    public Page queryMessageInfo(Map<String, Object> para,Page page) {

        int offset=(page.getCurrentPage()-1)*page.getPageSize();
        para.put("offset",offset);
        para.put("pageSize",page.getPageSize());
        List<MessageModel> messageList=messageDao.queryMessageInfo(para);
        int messageCount=messageDao.countQueryMessageInfo(para);
        page.setTotalCount(messageCount);
        page.setData(messageList);
        return page;
    }

    public Integer insertMessage(MessageModel model) {

        return messageDao.insertMessage(model);
    }



    public Integer deleteMessage(Map<String, Object> para) {
        return messageDao.deleteMessage(para);
    }

    @Override
    public Integer updateVersionInfo(Version vo) {
        return messageDao.updateVersionInfo(vo);
    }

    @Override
    public Version queryVersionInfo(Map<String, Object> para) {
        return messageDao.queryVersionInfo(para);
    }

    @Override
    public void sendMsg(String content, int frequency) throws NoSuchFieldException, IllegalAccessException {


        if( TaskUtil.getTimerList().size()>0){
            TaskUtil.getTimerList().get(0).cancel();
            TaskUtil.getTimerList().remove(0);

        }
        Timer timer = new Timer();
        MsgTask timeTask=new MsgTask("");
        timeTask.setContent(content);
        timer.scheduleAtFixedRate(timeTask, 0, frequency * 1000);
        TaskUtil.getTimerList().add(timer);
        PropertyUtil.writeProperties("content",content);
        PropertyUtil.writeProperties("frequency",frequency+"");
       /* TaskUtil.getTimeTask().setContent(content);
        if(TaskUtil.count==0){
            PropertyUtil.writeProperties("content",content);
            PropertyUtil.writeProperties("frequency",frequency+"");
            TaskUtil.getTimer().scheduleAtFixedRate(TaskUtil.getTimeTask(),0, frequency* 1000);
            TaskUtil.count++;
        }else{
            Date now = new Date();
            long nextExecutionTime = now.getTime() + NEW_CHECK_INTERVAL;
            TaskUtil.getTimeTask().setNxtExecutionTime(frequency*1000,nextExecutionTime);
//            TaskUtil.getTimeTask().setPeriod(frequency*1000);

        }*/


    }

    @Override
    public void stopMsg() {


        if( TaskUtil.getTimerList().size()>0){
            TaskUtil.getTimerList().get(0).cancel();
            TaskUtil.getTimerList().remove(0);

        }
        /*    Date now = new Date();
        long nextExecutionTime = now.getTime() + NEW_CHECK_INTERVAL;*/

//        TaskUtil.getTimeTask().setNxtExecutionTime(Long.MAX_VALUE*1000,nextExecutionTime);
//        TaskUtil.getTimeTask().setPeriod(Long.MAX_VALUE*1000);
    }
}
